class Manage::MetadataController < ApplicationController
  def contact_formats
    @mode = "list"
    case request.method
    when :post
      case params[:commit]
      when "Create"
        @contact_format = ContactFormat.new
        @mode = "create"
      when "Save"
        contact_format_post
        if @contact_format.save
          flash[:notice] = "Contact Format successfully updated" 
          @mode = "list"
        end
      end
    else
      contact_format_get
    end 
  end

  def user_address_types
    @mode = "list"
    case request.method
    when :post
      case params[:commit]
      when "Create"
        @user_address_type = UserAddressType.new
        @mode = "create"
      when "Save"
        user_address_type_post
        if @user_address_type.save
          flash[:notice] = "User Address Type successfully updated" 
          @mode = "list"
        end
      end
    else
      user_address_type_get
    end 
  end

  def user_contact_types
    @mode = "list"
    case request.method
    when :post
      case params[:commit]
      when "Create"
        @user_contact_type = UserContactType.new
        @mode = "create"
      when "Save"
        user_contact_type_post
        if @user_contact_type.save
          flash[:notice] = "User Contact Type successfully updated" 
          @mode = "list"
        end
      end
    else
      user_contact_type_get
    end 
  end

  private
    def contact_format_post
      @mode = params[:mode] 
      if params[:mode] == "create"
        @contact_format = ContactFormat.new
      else
        @contact_format = ContactFormat.find(params[:contact_format][:id])
      end
      params[:contact_format].delete(:id)
      @contact_format.attributes=(params[:contact_format])
    end

    def contact_format_get
      if params[:id] && params[:mode]
        @mode = params[:mode] 
        @contact_format = ContactFormat.find(params[:id].to_i) 
      end 
    end

    def user_address_type_post
      @mode = params[:mode] 
      if params[:mode] == "create"
        @user_address_type = UserAddressType.new
      else
        @user_address_type = UserAddressType.find(params[:user_address_type][:id])
      end
      params[:user_address_type].delete(:id)
      @user_address_type.attributes=(params[:user_address_type])
    end

    def user_address_type_get
      if params[:id] && params[:mode]
        @mode = params[:mode] 
        @user_address_type = UserAddressType.find(params[:id].to_i) 
        if @mode == "delete"
          if UserAddress.record_count_with_condition("user_address_type_id = #{params[:id].to_i}") > 0
            @user_address_type.errors.add_to_base("Cannot delete the #{@user_address_type.name} address type.  There are records with this type.  <br/>Please update and set Active? to false to prevent future use.")
          else
            @user_address_type.destroy
            @mode = "list"
          end
        end 
      end 
    end

    def user_contact_type_post
      @mode = params[:mode] 
      if params[:mode] == "create"
        @user_contact_type = UserContactType.new
      else
        @user_contact_type = UserContactType.find(params[:user_contact_type][:id])
      end
      params[:user_contact_type].delete(:id)
      @user_contact_type.attributes=(params[:user_contact_type])
    end

    def user_contact_type_get
      if params[:id] && params[:mode]
        @mode = params[:mode] 
        @user_contact_type = UserContactType.find(params[:id].to_i) 
        if @mode == "delete"
          if UserContact.record_count_with_condition("user_contact_type_id = #{params[:id].to_i}") > 0
            @user_contact_type.errors.add_to_base("Cannot delete the #{@user_contact_type.name} contact type.  There are records with this type.  <br/>Please update and set Active? to false to prevent future use.")
          else
            @user_contact_type.destroy
            @mode = "list"
          end
        end 
      end 
    end
end

